package com.siwei.mes.entity;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.common.annotation.AutoEnum;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.enums.GenerateCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;


/**
 * 工单(Order)实体类
 *
 * @author makejava
 * @since 2023-12-05 09:31:29
 */
@Data
@Table(name = "t_order")
@Accessors(chain = true)
@ApiModel(value = "Order", description = "工单")
public class Order implements Serializable {
    private static final long serialVersionUID = 283303948513226123L;
    /**
     * 主键id
     */
    @Id
    @ApiModelProperty(value = "工单主键ID", name = "id")
    private Long id;
    /**
     * 工单编号
     */
    @Column(name = "order_no")
    @ApiModelProperty(value = "工单编号", name = "orderNo")
    private String orderNo;
    /**
     * 工程id
     */
    @Column(name = "project_id")
    @ApiModelProperty(value = "工程id", name = "projectId")
    @NotNull(groups = {AddGroup.class, UpdateGroup.class}, message = "工程id为空")
    private Long projectId;
    /**
     * 工程名称
     */
    @Column(name = "project_name")
    @ApiModelProperty(value = "工程名称", name = "projectName")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "工程名称为空")
    private String projectName;
    /**
     * 合同id
     */
    @Column(name = "contract_id")
    @ApiModelProperty(value = "合同id", name = "contractId")
    @NotNull(groups = {AddGroup.class, UpdateGroup.class}, message = "合同id为空")
    private Long contractId;
    /**
     * 合同编号
     */
    @Column(name = "contract_no")
    @ApiModelProperty(value = "合同编号", name = "contractNo")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "合同编号为空")
    private String contractNo;
    /**
     * 合同名称
     */
    @Column(name = "contract_name")
    @ApiModelProperty(value = "合同名称", name = "contractName")
    // @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "合同名称为空")
    private String contractName;
    /**
     * 客户单位
     */
    @Column(name = "customer_name")
    @ApiModelProperty(value = "客户单位", name = "customerName")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "客户单位为空")
    private String customerName;
    /**
     * 工单类型  字典表 ORDER_TYPE
     */
    @Column(name = "order_type_code")
    @ApiModelProperty(value = "工单类型  字典表 ORDER_TYPE", name = "orderTypeCode")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "工单类型为空")
    private String orderTypeCode;
    /**
     * 工单类型名称
     */
    @Column(name = "order_type_name")
    @ApiModelProperty(value = "工单类型名称", name = "orderTypeName")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "工单类型名称为空")
    private String orderTypeName;
    /**
     * 工单状态  0-待处理  1-接受  2-完成  3-拒绝
     */
    @Column(name = "order_status")
    @ApiModelProperty(value = "工单状态  0-待处理  1-接受  2-完成  3-拒绝", name = "orderStatus")
    @AutoEnum(path = "com.siwei.mes.enums.OrderStatusEnum", fieldName = "orderStatusText")
    private Integer orderStatus;

    /**
     * 工单状态  0-待处理  1-接受  2-完成  3-拒绝
     */
    @Transient
    private String orderStatusText;
    /**
     * 工程师id  客服员
     */
    @Column(name = "user_id")
    @ApiModelProperty(value = "工程师id  客服员", name = "userId")
    @NotNull(groups = {AddGroup.class, UpdateGroup.class}, message = "客服员id为空")
    private Long userId;
    /**
     * 工程师名称  客服员名称
     */
    @Column(name = "user_name")
    @ApiModelProperty(value = "工程师名称  客服员名称", name = "userName")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "客服员名称为空")
    private String userName;
    /**
     * 开始服务时间
     */
    @Column(name = "start_service_time")
    @ApiModelProperty(value = "开始服务时间", name = "startServiceTime")
    private Date startServiceTime;
    /**
     * 服务完成时间
     */
    @Column(name = "finish_service_time")
    @ApiModelProperty(value = "服务完成时间", name = "finishServiceTime")
    private Date finishServiceTime;
    /**
     * 工程信息
     */
    @Column(name = "project_info")
    @ApiModelProperty(value = "工程信息", name = "projectInfo")
    private JSONObject projectInfo;
    /**
     * 小票id
     */
    @Column(name = "item_order_id")
    @ApiModelProperty(value = "小票id", name = "itemOrderId")
    private Long itemOrderId;
    /**
     * 创建人  派单人
     */
    @Column(name = "creater")
    @ApiModelProperty(value = "创建人  派单人", name = "creater")
    private Long creater;
    /**
     * 创建人名称  派单人名称
     */
    @ApiModelProperty(value = "创建人名称  派单人名称", name = "createrName")
    @Transient
    private String createrName;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @ApiModelProperty(value = "创建时间", name = "createTime")
    private Date createTime;
    /**
     * 修改人
     */
    @Column(name = "updater")
    @ApiModelProperty(value = "修改人", name = "updater")
    private Long updater;
    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @ApiModelProperty(value = "更新时间", name = "updateTime")
    private Date updateTime;
    /**
     * 是否删除  1-删除
     */
    @Column(name = "is_del")
    @ApiModelProperty(value = "是否删除  1-删除", name = "isDel")
    private Integer isDel;

    /**
     * 接收人id
     */
    @Column(name = "accept_id")
    @ApiModelProperty(value = "接收人id", name = "acceptId")
    private Long acceptId;

    /**
     * 接收人名称
     */
    @Column(name = "accept_name")
    @ApiModelProperty(value = "接收人名称", name = "acceptName")
    private String acceptName;

    /**
     * 接收时间
     */
    @Column(name = "accept_time")
    @ApiModelProperty(value = "接收时间", name = "acceptTime")
    private Date acceptTime;

    /**
     * 完成人id
     */
    @Column(name = "finish_id")
    @ApiModelProperty(value = "完成人id", name = "finishId")
    private Long finishId;

    /**
     * 完成人名称
     */
    @Column(name = "finish_name")
    @ApiModelProperty(value = "完成人名称", name = "finishName")
    private String finishName;

    /**
     * 完成时间
     */
    @Column(name = "finish_time")
    @ApiModelProperty(value = "完成时间", name = "finishTime")
    private Date finishTime;

    /**
     * 拒绝人id
     */
    @Column(name = "refuse_id")
    @ApiModelProperty(value = "拒绝人id", name = "refuseId")
    private Long refuseId;

    /**
     * 拒绝人名称
     */
    @Column(name = "refuse_name")
    @ApiModelProperty(value = "拒绝人名称", name = "refuseName")
    private String refuseName;

    /**
     * 拒绝时间
     */
    @Column(name = "refuse_time")
    @ApiModelProperty(value = "拒绝时间", name = "finishTime")
    private Date refuseTime;

    /**
     * 拒绝原因
     */
    @Column(name = "refuse_reason")
    @ApiModelProperty(value = "拒绝原因", name = "refuseReason")
    private String refuseReason;

    @Transient
    @ApiModelProperty(value = "工单详情")
    private OrderDetail orderDetail;

    @Transient
    @ApiModelProperty(value = "关键字搜索")
    private String keyword;

    @Transient
    @ApiModelProperty("新增工单详情的JSON")
    private JSONObject detailInfo;

    @Transient
    @ApiModelProperty(value = "新增工单详情的图片或视频")
    private JSONObject detailFileInfo;

    @Transient
    @ApiModelProperty("状态数组")
    private List<String> statusList;

    @Transient
    @ApiModelProperty("打卡地址,取最近一次")
    private String clockAddress;

    @Transient
    @ApiModelProperty("派单人电话")
    private String createrPhone;

    @Transient
    @ApiModelProperty("客服员电话号码")
    private String userPhone;

    public Order init(boolean isCreate) {
        this.setUpdater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null)
                .setUpdateTime(new Date());
        if (isCreate) {
            this.setCreater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null)
                    .setCreateTime(new Date()).setOrderStatus(0)
                    .setIsDel(0).setOrderNo(UidUtils.genNo(GenerateCodeEnum.ORDER.getCode()));
        }
        return this;
    }
}
